<?php
/**
生成下载文件的方法：
$row = ['标题','内容'];
myexcel::writeRow($row);
myexcel::download();
 * */
require_once __DIR__."/PHPExcel-1.8/Classes/PHPExcel.php";//引入读取excel类文件

class myexcel{
	public static $instant;
	public static $currentExcelRow=1;
	public static $fileName='excel.xlsx';

    public static function setProperties(){
        $objPHPExcel = self::getInstant();

        // Set document properties
        $objPHPExcel->getProperties()->setCreator("LikeCTO")
            ->setLastModifiedBy("LikeCTO")
            ->setTitle("Office 2007 XLSX LikeCTO Document")
            ->setSubject("Office 2007 XLSX LikeCTO Document")
            ->setDescription("LikeCTO document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("LikeCTO result file");
    }

    public static function setFileName($fileName){
        self::$fileName = $fileName;
    }

    public static function getFileName(){
        return self::$fileName;
    }

	public static function getInstant(){
        if(!self::$instant){
            self::$instant = new  PHPExcel();
        }
        return self::$instant;
    }

	public static  function read($fileName,$sheetName='Sheet1'){
		//$filename = __DIR__.'/demo.xlsx';//需要导入的excel路径


		//1.部分加载
		$file_type = PHPExcel_IOFactory::identify($fileName);//读取文件类型
		$obj_reader = PHPExcel_IOFactory::createReader($file_type);//获取文件操作对象
		$obj_reader->setLoadSheetsOnly($sheetName);//只读取sheet的名称 多个可以用数组array(sheet1,sheet2...)@todo 可以自动识别
		$obj_PHPExcel = $obj_reader->load($fileName);//加载文件

		//2.全部加载
		$obj_PHPExcel = PHPExcel_IOFactory::load($fileName); //加载文件
		$sheet_count = $obj_PHPExcel->getSheetCount(); //获取文件sheet数量

		//1.全部读取
        for ($i=0; $i < $sheet_count; $i++) {
            $data[$i] = $obj_PHPExcel->getSheet($i)->toArray();//读取每个sheet的数据放入数组中
        }
		return $data;


		/*2.逐行读取*/
		/*foreach ($obj_PHPExcel->getWorksheetIterator() as $sheet) { //循环读取sheet
			foreach ($sheet->getRowIterator() as $row) { //逐行处理
				//去除头部，只显示数据
				if ($row->getRowIndex() < 2 ){
					continue;
				}

				foreach ($row->getCellIterator() as $cell) { //逐列获取
					$data = $cell->getValue();//读取单元格数据
					echo $data.' ';
				}
				echo "<br>";
			}
			echo "<br>";
		}*/
 	}

    public static function save(){
        self::download();
    }

	public static function download(){
        self::setProperties();
        $objPHPExcel = self::getInstant();
        // Redirect output to a client’s web browser (Excel2007)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.self::getFileName().'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
	}

    public static function setSheetTitle($sheetTitle='Sheet1',$sheetIdx=0){
        $objPHPExcel = self::getInstant();
        $objPHPExcel->setActiveSheetIndex($sheetIdx);

        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle($sheetTitle);
    }

    public static function createSheet($iSheetIndex=null){
        $objPHPExcel = self::getInstant();
        self::$currentExcelRow = 1;
        $newSheet = new PHPExcel_Worksheet($objPHPExcel);
        $objPHPExcel->addSheet($newSheet, $iSheetIndex);
        return $newSheet;
    }
    public static function addSheet($iSheetIndex=null){
       return self::createSheet($iSheetIndex);
    }

	public static function writeRow($row,$sheetIdx=0){
        // Create new PHPExcel object
		$objPHPExcel = self::getInstant();
        for($i = 0; $i < count($row); $i++){
            $str = self::intToChr($i);
            $pos = $str.self::$currentExcelRow;
            $val = $row[$i];

            $objPHPExcel->setActiveSheetIndex($sheetIdx)
                ->setCellValue($pos, $val);
        }
        self::$currentExcelRow++;
	}

    public static function intToChr($index, $start = 65) {
        $str = '';
        if (floor($index / 26) > 0) {
            $str .= IntToChr(floor($index / 26)-1);
        }
        return $str . chr($index % 26 + $start);
    }




}
